﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace Grafica3
{
    public class Floor
    {
        VertexBuffer vb;
        IndexBuffer ind;
        Texture2D text;
        Effect fx;
        GraphicsDevice graphic;
        ContentManager cont;

        public Floor(GraphicsDevice graphicDevice, ContentManager content, Effect effect, PointLight[] PointLights)
        {
            text = content.Load<Texture2D>("FloorText"); //Water_Sparkling
            fx = effect.Clone();

            graphic = graphicDevice;

            Vector3 N = Vector3.Up;

            VertexPositionNormalTexture[] points = new VertexPositionNormalTexture[]
            {
                new VertexPositionNormalTexture(new Vector3(1, 0, 1), N, new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1, 0, -1), N, new Vector2(1, -1)),
                new VertexPositionNormalTexture(new Vector3(-1, 0, -1), N, new Vector2(-1, -1)),
                new VertexPositionNormalTexture(new Vector3(-1, 0, 1), N, new Vector2(-1, 1))
            };

            var indices = new Int16[] { 0, 1, 2, 0, 2, 3};

            vb = new VertexBuffer(graphic, VertexPositionNormalTexture.VertexDeclaration, points.Length, BufferUsage.WriteOnly);
            vb.SetData(points);

            ind = new IndexBuffer(graphic, IndexElementSize.SixteenBits, indices.Length, BufferUsage.WriteOnly);
            ind.SetData(indices);

            var bf = new BasicEffect(graphic);
            bf.EnableDefaultLighting();

            fx.Parameters["SpecularPower"].SetValue(bf.SpecularPower);
            fx.Parameters["SpecularColor"].SetValue(Color.White.ToVector4());
            fx.Parameters["DiffuseColor"].SetValue(new Vector4(bf.DiffuseColor, 255));
            fx.Parameters["AmbientLightColor"].SetValue(new Vector4(bf.AmbientLightColor, 255));

            for (int i = 0; i < PointLights.Count(); i++)
            {
                fx.Parameters["PointLight" + i].StructureMembers["Position"].SetValue(new Vector4(PointLights[i].Position, 1));
                fx.Parameters["PointLight" + i].StructureMembers["DiffuseColor"].SetValue(PointLights[i].DiffuseColor.ToVector4());
                fx.Parameters["PointLight" + i].StructureMembers["SpecularColor"].SetValue(PointLights[i].SpecularColor.ToVector4());
                fx.Parameters["PointLight" + i].StructureMembers["Enabled"].SetValue(true);
            }

            //fx.Parameters["Direction0"].SetValue(bf.DirectionalLight0.Direction);
            //fx.Parameters["DiffuseColor0"].SetValue(new Vector4(bf.DirectionalLight0.DiffuseColor, 255));
            //fx.Parameters["SpecularColor0"].SetValue(new Vector4(bf.DirectionalLight0.SpecularColor, 255));

            //fx.Parameters["Direction1"].SetValue(bf.DirectionalLight1.Direction);
            //fx.Parameters["DiffuseColor1"].SetValue(new Vector4(bf.DirectionalLight1.DiffuseColor, 255));
            //fx.Parameters["SpecularColor1"].SetValue(new Vector4(bf.DirectionalLight1.SpecularColor, 255));

            //fx.Parameters["Direction2"].SetValue(bf.DirectionalLight2.Direction);
            //fx.Parameters["DiffuseColor2"].SetValue(new Vector4(bf.DirectionalLight2.DiffuseColor, 255));
            //fx.Parameters["SpecularColor2"].SetValue(new Vector4(bf.DirectionalLight2.SpecularColor, 255));

            fx.Parameters["Diffuse"].SetValue(text);

            bf.Dispose();
            cont = content;
        }

        public void Draw(Camera camera, IEnumerable<PointLight> lights)
        {
            var shadowMaps = (from l in lights select l.ShadowMap).ToArray();
            var LVPs = (from l in lights select l.CreateLightViewProjectionMatrix(camera)).ToArray();

            for (int i = 0; i < shadowMaps.Length; i++)
            {
                fx.Parameters["LightVP" + i].SetValue(LVPs[i]);
                fx.Parameters["ShadowLight" + i].SetValue(shadowMaps[i]);
            }

            graphic.SetVertexBuffer(vb);
            graphic.Indices = ind;
            fx.Parameters["World"].SetValue(Matrix.CreateScale(100));
            fx.Parameters["View"].SetValue(camera.View);
            fx.Parameters["Projection"].SetValue(camera.Projection);
            fx.Parameters["TextureEnabled"].SetValue(true);
            fx.Parameters["ShadowEnabled"].SetValue(true);

            fx.CurrentTechnique.Passes[0].Apply();
            graphic.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vb.VertexCount, 0, ind.IndexCount / 3);

        }



    }
}
